import vue from "@vitejs/plugin-vue";
import { codeInspectorPlugin } from "code-inspector-plugin";
import { resolve } from "path";
import UnoCss from "unocss/vite";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [
    vue(),
    codeInspectorPlugin({
      bundler: "vite"
    }),
    UnoCss({ configFile: "./uno.config.ts" }),
    Components({
      // 自动加载的组件目录，默认值为 ['src/components']
      dirs: ["src/components"],
      // 组件名称包含目录，防止同名组件冲突
      directoryAsNamespace: false,
      // 指定类型声明文件，为true时在项目根目录创建
      dts: "./components.d.ts"
    }),
    AutoImport({
      imports: [
        "vue",
        "vue-router",
        "@vueuse/core",
        {
          axios: [["default", "axios"]]
        },
        {
          nanoid: ["nanoid"]
        }
      ],
      dts: "./auto-imports.d.ts",
      vueTemplate: true,
      dirs: ["src/hook", "src/api/service"]
    })
  ],
  resolve: {
    alias: [
      {
        find: "@",
        replacement: resolve(__dirname, "./src")
      },
      {
        find: "assets",
        replacement: resolve(__dirname, "./src/assets")
      },
      {
        find: "vue-i18n",
        replacement: "vue-i18n/dist/vue-i18n.cjs.js" // Resolve the i18n warning issue
      },
      {
        find: "vue",
        replacement: "vue/dist/vue.esm-bundler.js" // compile template
      }
    ],
    extensions: [".ts", ".js"]
  },
  define: {
    "process.env": {}
  },
  css: {
    preprocessorOptions: {
      scss: {
        api: "modern-compiler", // or 'modern'
        silenceDeprecations: ["legacy-js-api"]
      }
    }
  },
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          arco: ["@arco-design/web-vue"],
          chart: ["echarts"],
          vue: ["vue", "vue-router", "pinia", "@vueuse/core", "vue-i18n"]
        }
      }
    },
    chunkSizeWarningLimit: 2000
  },
  server: {
    open: true,
    // fs: {
    //   strict: true
    // },
    port: 9999
  },
  /* 切换分支时，会导致浏览器找不到Vite相关文件，需要排除 */
  optimizeDeps: {
    exclude: [
      "chunk-NWF4X5GZ",
      "chunk-CCEL3GHP",
      "chunk-ZUMGVBFX",
      "chunk-XR6X47R7",
      "chunk-BDNKYNRE",
      "chunk-F5AIDYMH",
      "chunk-5ZY3I6JC",
      "chunk-NM6B6QPK",
      "chunk-2M5QIUPN",
      "chunk-WQJF76DH",
      "chunk-SA3NQFXU",
      "chunk-X6COC45P",
      "chunk-23EU7ABK" //在这里去除对应的文件
    ] // 将问题依赖项添加到 exclude 中
  }
});
